import os

# 定义要处理的目录和要添加的注释
target_directory = 'code'
author_comment = "# 数据分析题目解答(建议先赞后看，养成习惯 如果不赞，先拉出去枪毙两分钟 作者：小匠IT)\n"

# 遍历目标目录
for filename in os.listdir(target_directory):
    if filename.endswith('.py'):
        file_path = os.path.join(target_directory, filename)
        
        # 读取文件内容
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
        
        # 检查文件是否已经有该注释，避免重复添加
        if author_comment not in content:
            # 在文件开头添加注释
            new_content = author_comment + content
            
            # 写入新的内容到文件中
            with open(file_path, 'w', encoding='utf-8') as file:
                file.write(new_content)

print("已成功为code目录下的.py文件添加了作者注释。")